<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('reviewBatch.fields.batchName')" prop="batchName">
        <el-input
          v-model="queryParams.batchName"
          :placeholder="t('common.inputTextPlaceholder') + t('reviewBatch.fields.batchName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching"
          ><Icon icon="ep:search" class="mr-5px" />{{ t('common.search') }}</el-button
        >
        <el-button @click="resetQuery" :disabled="advancedSearching"
          ><Icon icon="ep:refresh" class="mr-5px" />{{ t('common.reset') }}</el-button
        >
        <el-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" />
          {{ t('action.add') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iReviewBatch-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('reviewBatch.fields.batchName')"
        field="batchName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewBatch.fields.year')"
        field="year"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewBatch.fields.applyStartTime')"
        field="applyStartTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewBatch.fields.applyEndTime')"
        field="applyEndTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewBatch.fields.reviewStartTime')"
        field="reviewStartTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewBatch.fields.reviewEndTime')"
        field="reviewEndTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewBatch.fields.applyCount')"
        field="applyCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('reviewBatch.fields.reviewCount')"
        field="reviewCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('reviewBatch.fields.votedCount')"
        field="votedCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('reviewBatch.fields.unVotedCount')"
        field="unVotedCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="150"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleSetReviewer(scope.row)">
            {{ t('reviewBatch.index.operate.setReviewer') }}
          </el-button>
          <el-button link type="primary" @click="gotoDetail(scope.row.id)">
            {{ t('reviewBatch.index.operate.applyDetail') }}
          </el-button>
          <el-button link type="primary" @click="handleExportPassed(scope.row)">
            {{ t('reviewBatch.index.operate.exportPassed') }}
          </el-button>
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            {{ t('action.edit') }}
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">
            {{ t('action.delete') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ReviewBatchForm ref="formRef" @success="getList" />
  <!-- 设置招生资格审查评委 -->
  <SetReviewBatchMentor ref="setReviewBatchMentorRef" @update="getList" />
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import { ReviewBatchApi, ReviewBatchVO } from '@/api/recruit/reviewbatch'
import ReviewBatchForm from './ReviewBatchForm.vue'
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import download from '@/utils/download'
import { ElLoading } from 'element-plus'
import SetReviewBatchMentor from './SetReviewBatchMentor.vue'
import { QualificationReviewDetailApi } from '@/api/recruit/qualificationReviewDetail'

/** 招生资格审查批次 列表 */
defineOptions({ name: 'ReviewBatch' })

const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<ReviewBatchVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchName: undefined
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ReviewBatchApi.getReviewBatchPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof ReviewBatchForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ReviewBatchApi.deleteReviewBatch(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

// 设置评委
const setReviewBatchMentorRef = ref<InstanceType<typeof SetReviewBatchMentor>>()
const handleSetReviewer = (item: ReviewBatchVO) => {
  setReviewBatchMentorRef.value?.open(item.id)
}

// 进入资审批次申请明细页
const gotoDetail = (id: number) => {
  router.push({
    path: `/recruit/review-batch/${id}`
  })
}

// 导出已通过
const handleExportPassed = async (item: ReviewBatchVO) => {
  let loading: any = undefined
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    loading = ElLoading.service({
      lock: true,
      text: t('common.exporting'),
      background: 'rgba(0, 0, 0, 0.7)'
    } as any)
    const data = await QualificationReviewDetailApi.exportReviewDetail(item.id, true)
    download.excel(data, `${item.batchName}-招生资格审查-已通过名单.xlsx`)
  } catch {
    message.error(t('common.exportFailed'))
  } finally {
    loading?.close()
  }
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
